package com.check;

import com.check.utils.PropertiesUtils;
import com.check.utils.Rule;

import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @description:
 * @Author: zyt
 */
public enum CheckEnum {
    EMAIL("邮箱"){
        @Override
        public boolean check(String value) {
            return rule.emailRule(value);
        }
    },
    IDCV("身份证"){
        @Override
        public boolean check(String value) {
            return Rule.IdentityCardVerification(value);
        }
    },PHONE("手机号"){
        @Override
        public boolean check(String value) {
            return rule.phoneRule(value);
        }
    },ONE("唯一性"){
        @Override
        public boolean check(String value) {
            return rule.isOne(value);
        }
    },DOUBLE("小数"){
        @Override
        public boolean check(String value) {
            return rule.isDoubleNum(value);
        }
    },INTNUM("整数"){
        @Override
        public boolean check(String value) {
            return rule.isIntNum(value);
        }
    },NOTNULL("必填项"){
        @Override
        public boolean check(String value) {
            return rule.isNotNull(value);
        }
    },PHONEALL("手机加座机"){
        @Override
        public boolean check(String value) {
            return rule.phoneAllRule(value);
        }
    },DATE("日期"){
        @Override
        public boolean check(String value) {
            return rule.isDate(value);
        }
    },DATETIME("日期加时间"){
        @Override
        public boolean check(String value) {
            return rule.isDateTime(value);
        }
    },NUMANDABC("数字加字母"){
        @Override
        public boolean check(String value) {
            return rule.isNumAndAbc(value);
        }
    },NOTZH("非中文"){
        @Override
        public boolean check(String value) {
            return rule.isNotAllChinese(value);
        }
    },ZDY("ZDY"){

        @Override
        public String check(String key, String value, List<List<String>> errList, List<String> list, AtomicReference<Boolean> b,Rule rule) {
            this.rule = rule;
            String message = "";
            Properties properties = PropertiesUtils.getPropertiesValue("/checkRule.properties");
            if(properties.containsKey(key)){
                value = value.trim();
                final String ruleValue = properties.getProperty(key);
                if (ruleValue.startsWith("!@@")){
                    int left = Integer.parseInt(ruleValue.substring(3,ruleValue.indexOf(":")));
                    int right = Integer.parseInt(ruleValue.substring(ruleValue.indexOf(":")+1));
                    if (!rule.isIntNum(value)){
                        message = "非"+key+";";
                        if(b.get()){
                            errList.add(list);
                            b.set(false);
                        }
                        return message;
                    }
                    int valueInt = Integer.parseInt(value);
                    if (!(valueInt>=left && valueInt<=right)){
                        message = "非"+key+";";
                        if(b.get()){
                            errList.add(list);
                            b.set(false);
                        }
                    }
                    return message;
                }

                if(!rule.matches(ruleValue,value)){
                    if(b.get()){
                        errList.add(list);
                        b.set(false);
                    }
                    message="非"+key+";";
                }
            }
            return message;
        }
    };


    Rule rule = new Rule();

    private String name;
    CheckEnum(String name){
        this.name = name;
    }

    public static CheckEnum getByName(String name){
        CheckEnum[] values = CheckEnum.values();
        for (CheckEnum value:values){
            if (value.name == name){
                return value;
            }
        }
        return CheckEnum.ZDY;
    }

    /**
     * 检查逻辑
     * @param key
     * @param value
     * @param errList
     * @param list
     * @param b
     * @param rule
     * @return
     */
    public String check(String key, String value, List<List<String>> errList,
                        List<String> list, AtomicReference<Boolean> b,Rule rule){
        this.rule = rule;
        String message = "";
        if(this.name.equals(key)){
            if (!this.check(value)) {
                if(b.get()){
                    errList.add(list);
                    b.set(false);
                }
                message=name+";";
            }
        }
        return message;
    }

    /**
     * 判断是否相同
     * @param value
     * @return
     */
    public boolean check(String value){
        return false;
    }
}
